package cn.tedu.csmall.commons.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.web.util.pattern.PathPattern;

import java.util.Date;
import java.util.Map;

public class JwtUtils {

    //定义密钥
    private static  final String SECRET_KEY="AHJHKDHADKLAJSDLAOQIWUEO";
    //定义有效期
    private static  final Long EXPIRED_IN_MINUTE=24l*60*7;

    //私有化构造方法
    private JwtUtils(){}

    //生成JWT的方法
    public static String generate(Map<String,Object> claims){
        Date date = new Date(System.currentTimeMillis() + EXPIRED_IN_MINUTE * 60 * 1000);

        String jwt = Jwts.builder()
                .setHeaderParam("alg", "HS256")
                .setHeaderParam("typ", "jwt")
                .setClaims(claims)
                .setExpiration(date)
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
        return jwt;
    }

    //解析JWT的方法
    public static Claims parse(String jwt){
        Claims claims=Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
